<?php
// +----------------------------------------------------------------------
// | A3Mall
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xzncit <158373108@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\job;

use app\admin\controller\Auth;
use mall\basic\Attachments;
use mall\utils\Data;
use mall\utils\Tool;
use mall\response\Response;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use app\common\model\job\JobType;
use app\common\model\job\Job;

class Category extends Auth {

    public function index(){

        if(Request::isAjax()) {
            $limit = Request::get("limit");
            $condition = ["pid"=>0];
            $jobType = new JobType();
            $list = $jobType->getList($condition,$limit);
            if(empty($list['data'])){
                return Response::returnArray("当前还没有数据哦！",1);
            }
            return Response::returnArray("ok",0,$list['data'],$list['count']);
        }

        return View::fetch();
    }

    public function editor(){
        if(!Request::isAjax()){
            $id = (int)Request::param("id");
            $rs = empty($id) ? [] : JobType::find($id);
            $list = JobType::getAvailableTypeList();
            $cat = Data::analysisTree(Data::familyProcess($list));
            if($rs){
                $icon_name=basename($rs['icon']);
                $icon=Db::name("attachments")->where('name',$icon_name)->select()->toArray();                
            }
            return View::fetch("",[
                "cat"=>$cat,
                "data"=>$rs,
                'icon'=>$icon[0] ?? [],
            ]);
        }

        $data = Request::post();
        $jobType = new JobType();
        //附件
        $attachment_id = empty($data['attachment_id']) ? 0 : $data['attachment_id'];
        if($attachment_id){
            $data['icon']=Db::name("attachments")->where('id',$attachment_id)->value('path');
        }
        if(($obj=$jobType::find($data["id"])) != false){
            if(!$jobType->check_tree($jobType->where(["status"=>1])->select()->toArray(),$data)){
                return Response::returnArray("{$data['name']} 是 ID {$data['pid']} 的父栏目,不能修改！", 0);
            }

            try {
                $obj->save($data);
            } catch (\Exception $ex) {
                return Response::returnArray("操作失败，请重试。",0);
            }
        }else{
            try{
                $jobType->save($data);
            }catch (\Exception $e){
                return Response::returnArray("操作失败，请重试。",0);
            }

            $data['id'] = $jobType->id;
        }
        return Response::returnArray("操作成功！");
    }

    public function delete(){
        if(!Request::isAjax()){
            return Response::returnArray("本页面不允许直接访问！",0);
        }

        $id = (int)Request::get("id");
        try {
            // 检测是否有子分类
            if(JobType::where("pid",$id)->count() > 0){
                return Response::returnArray("该分类下有子分类，请先删除子分类。",0);
            }

            // 检测是否有商品
            if(Job::where("type_id",$id)->count() > 0){
                return Response::returnArray("该分类下有任务，请先删除任务。",0);
            }

            JobType::where('id',$id)->delete();
        } catch (\Exception $ex) {
            return Response::returnArray("操作失败，请稍候在试。",0);
        }

        return Response::returnArray("ok");
    }

    public function field(){
        if(!Request::isAjax()){
            return Response::returnArray("本页面不允许直接访问！",0);
        }

        $id = (int)Request::get("id");
        $name = strip_tags(trim(Request::get("name")));
        $value = (int)Request::get("value");

        try {
            JobType::where("id",$id)->update([$name=>$value]);
        } catch (\Exception $ex) {
            return Response::returnArray("操作失败，请稍候在试！",0);
        }

        return Response::returnArray("ok");
    }

}
